package com.backend.system.service.impl;

import com.backend.system.beans.user.UserQueryReq;
import com.backend.system.beans.user.UserQueryVO;
import com.backend.system.common.DateRange;
import com.backend.system.common.PageBean;
import com.backend.system.common.enums.SexEnum;
import com.backend.system.dao.entity.User;
import com.backend.system.dao.mapper.UserMapper;
import com.backend.system.service.IUserService;
import com.backend.system.util.PageUtil;
import com.backend.system.util.StringUtil;
import com.backend.system.util.TimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author backend-manage
 * @since 2024-04-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    /**
     * 通过登录信息查找用户数据
     *
     * @param loginAccount    登录信息-账号
     * @param encryptPassword 登录信息-加密后的密码
     * @return 用户信息
     */
    @Override
    public User searchUserByLoginInfo(String loginAccount, String encryptPassword) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getLoginAccount, loginAccount);
        queryWrapper.eq(User::getPassword, encryptPassword);
        return this.getOne(queryWrapper);
    }

    /**
     * 查询用户列表
     *
     * @param req 查询参数
     * @return 响应结果
     */
    @Override
    public PageBean<UserQueryVO> listByPage(UserQueryReq req) {
        // 分页查询
        IPage<User> page = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<User> queryWrapper = buildQueryWrapper(req);
        IPage<User> userPage = this.getBaseMapper().selectPage(page, queryWrapper);

        // 处理查询结果
        List<User> records = userPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageBean<>();
        }
        List<UserQueryVO> resultList = records.stream().map(UserQueryVO::convert).collect(Collectors.toList());
        return PageUtil.convertPage(userPage, resultList);
    }

    private LambdaQueryWrapper<User> buildQueryWrapper(UserQueryReq req) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(req.getUserId()), User::getId, req.getUserId());
        queryWrapper.eq(StringUtil.isNotBlank(req.getUsername()), User::getUsername, req.getUsername());

        SexEnum sexEnum = SexEnum.getInstance(req.getSex());
        queryWrapper.eq(Objects.nonNull(sexEnum), User::getSex, sexEnum);

        queryWrapper.eq(Objects.nonNull(req.getBirthday()), User::getBirthday, req.getBirthday());
        DateRange registerDate = req.getRegisterDate();
        if (Objects.nonNull(registerDate)) {
            LocalDate start = registerDate.getStart();
            if (Objects.nonNull(start)) {
                queryWrapper.ge(User::getCreateTime, TimeUtil.getDayBeginDateTime(start));
            }

            LocalDate end = registerDate.getEnd();
            if (Objects.nonNull(end)) {
                queryWrapper.le(User::getCreateTime, TimeUtil.getDayEndDateTime(end));
            }
        }
        queryWrapper.orderByDesc(User::getCreateTime);
        return queryWrapper;
    }

}
